#!bin/bash
# 从人民网获取24、48小时天气数据
if [ ! -f /tmp/gz_weather ]; then
   ping -c1 121.14.89.14 >> /dev/null
   if [ $? -eq 0 ]; then
	# 华南地区： 广州 353-359行  深圳 362-368行  汕头 371-377行 
	# 华中地区： 郑州 381-387行  武汉 392-398行  
	links -no-g -source "http://card.people.com.cn/forecast/inland24.jsp?aboard=0&interval=24"|sed -n '353,359p'|iconv -f GB18030 -t UTF8 |cut -d\> -f3|sed 's/<\/font//'|sed 's/&nbsp;//'|tr -d '\<' > /tmp/weather
	links -no-g -source "http://card.people.com.cn/forecast/inland24.jsp?aboard=0&interval=48"|sed -n '353,359p'|iconv -f GB18030 -t UTF8 |cut -d\> -f3|sed 's/<\/font//'|sed 's/&nbsp;//'|tr -d '\<' >> /tmp/weather
	ipaddress=`links -no-g -dump http://www.ip138.com/ip2city.asp | awk 'BEGIN{FS="["; RS="]"}$NF&&$NF!~/.*=.*/{print $2}'`
	location=`sed -n '1p' /tmp/weather`
	dayweather24=`sed -n '2p' /tmp/weather`
	daywind24=`sed -n '3p' /tmp/weather`
	daytemp24=`sed -n '4p' /tmp/weather|sed 's/[^0-9]//'`
	nightweather24=`sed -n '5p' /tmp/weather`
	nightwind24=`sed -n '6p' /tmp/weather`
	nighttemp24=`sed -n '7p' /tmp/weather|sed 's/[^0-9]//'`
	dayweather48=`sed -n '9p' /tmp/weather`
	daywind48=`sed -n '10p' /tmp/weather`
	daytemp48=`sed -n '11p' /tmp/weather|sed 's/[^0-9]//'`
	nightweather48=`sed -n '12p' /tmp/weather`
	nightwind48=`sed -n '13p' /tmp/weather`
	nighttemp48=`sed -n '14p' /tmp/weather|sed 's/[^0-9]//'`
	if [ $daytemp24 -lt $nighttemp24 ]; then
		echo "$location地区未来24小时天气情况" > /tmp/gz-weather
		echo "白天 $dayweather24	$daywind24 最高气温:$daytemp24℃" >> /tmp/gz-weather
		echo "晚间 $nightweather24	$nightwind24 最低气温:$nighttemp24℃" >> /tmp/gz-weather
		echo "$location地区未来48小时天气情况" >> /tmp/gz-weather
		echo "白天 $dayweather48	$daywind48 最高气温:$daytemp48℃" >> /tmp/gz-weather
		echo "晚间 $nightweather48	$nightwind48 最低气温:$nighttemp48℃" >> /tmp/gz-weather
	else
		echo "$location地区未来24小时天气情况" > /tmp/gz-weather
		echo "白天 $dayweather24	$daywind24 最高气温:$daytemp24℃" >> /tmp/gz-weather
		echo "晚间 $nightweather24	$nightwind24 最低气温:$nighttemp24 ℃" >> /tmp/gz-weather
      		echo "$location地区未来48小时天气情况" >> /tmp/gz-weather
		echo "白天 $dayweather48	$daywind48 最高气温:$daytemp48℃" >> /tmp/gz-weather
		echo "晚间 $nightweather48	$nightwind48 最低气温:$nighttemp48℃" >> /tmp/gz-weather
	fi
	echo "本机IP地址：" >> /tmp/gz-weather
	echo "$ipaddress" >> /tmp/gz-weather
	rm -rf /tmp/weather
   else
	if [ ! -f /tmp/pppoeconf ]; then 
	   gxmessage -center -bg "#ddff00" -timeout 3 -title "未联网" -buttons "" -encoding UTF-8\
            "  本机无联网，无法获取IP及天气预报信息，请先设置网络!
               现在调用网络配置工具配置ADSL网络,如果仍然无法成功，
               请自行运行sudo tkpppoe来配置。
            " &
         sleep 5 && touch /tmp/pppoeconf && exec sudo /usr/bin/pppoeconf >>/dev/null
	   ping -c1 121.14.89.14 >> /dev/null
         if [ $? -ne 0 ]; then
		gxmessage -center -bg "#ddff00" -timeout 3 -title "未联网" -buttons "" -encoding UTF-8\
             "  ppoeconf无法配置网络，
                    现在调用RP-PPPoE来配置网络。
                " &
             sleep 5 && touch /tmp/pppoeconf && exec sudo /usr/bin/tkpppoe
	       ping -c1 121.14.89.14 >> /dev/null
             if [ $? -ne 0 ]; then
                gxmessage -center -bg "#ddff00" -timeout 8 -title "未联网" -buttons "" -encoding UTF-8\
                    "RP-PPPoE也无法配置网络，非PPPoE网络，请自行配置。"
             fi
         fi
       fi
   fi
fi

	